// helloworld
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int var = 100;

int main() {

	char *p;
	int fd = open("tmp", O_RDWR | O_CREAT | O_TRUNC, 0755);
	if (-1 == fd) {
		perror("open error\n");
		close (fd);
		return 0;
	}

	unlink("tmp");

	ftruncate(fd, 1024);

	p = (char *) mmap(0, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
	// p = (char *) mmap(0, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (MAP_FAILED == p) {
		perror("mmap error");
		close (fd);
		return 0;
	}

	close(fd);

	pid_t pid = fork();

	if (pid == 0) {
		strcpy(p, "HelloWorld");
		var = 200;
		printf("child *p = '%s', var = %d\n", p, var);
	} else if (pid > 0) {
		wait(0);
		printf("parent *p = '%s', var = %d\n", p, var);
		munmap(p, 1024);
	} else {
		perror("helloworld");
	}

	return 0;
}
